﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Memo.Electricity.Game;

public class ElectricityManager : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]
    private GameObject mElectricity;
	[SerializeField]
	private GameObject mDisapearLeft;
	[SerializeField]
	private GameObject mDisapearRight;
	[SerializeField]
	private GameObject mDisapearMiddle;
	[HideInInspector]
	private List<Transform> transforms;

	// Update is called once per frame
	private struct LightningInfo
	{
		public Transform start;
		public Transform end;
	}
	private Dictionary<LightningInfo, GameObject> mLightningCache;


	private void Awake()
	{
		mLightningCache = new Dictionary<LightningInfo, GameObject>();
	}

	// Update is called once per frame
	private void Update()
	{
		transforms = CRelay.ConnectPath;
		Dictionary<LightningInfo, GameObject> newCache = new Dictionary<LightningInfo, GameObject>();
		if (transforms != null)
		{
			for (int i = 0; i < transforms.Count - 1; i++)
			{
				LightningInfo info = new LightningInfo();
				info.start = transforms[i];
				info.end = transforms[i + 1];
				if (!mLightningCache.ContainsKey(info))
				{
					var go = MakeElectricity(info.start, info.end);
					if (go != null)
					{
						newCache.Add(info, go);
					}
						
				}
				else
				{
					newCache.Add(info, mLightningCache[info]);
				}
			}
		}

		if (newCache.Count == 0)
		{
			if (mLightningCache.Count != 0)
			{
				GameObject left = Instantiate(mDisapearLeft);
				GameObject right = Instantiate(mDisapearRight);

				Transform t1 = PlayerManager.Inst.Player1.transform;
				Transform t2 = PlayerManager.Inst.Player2.transform;
				if (t1.position.x > t2.position.x)
				{
					Transform temp = t1;
					t1 = t2;
					t2 = temp;
				}
				left.transform.position = t1.position + left.GetComponent<ElectricityDisapear>().mOffset;
				right.transform.position = t2.position + right.GetComponent<ElectricityDisapear>().mOffset;
				left.transform.parent = t1;
				right.transform.parent = t2;

				foreach (var key in mLightningCache.Keys)
				{
					MakeElectricityDisapear(key.start, key.end);
					Destroy(mLightningCache[key]);
				}
			}
		}
		else
		{
			foreach (var key in mLightningCache.Keys)
			{
				if (!newCache.ContainsKey(key))
				{
					Destroy(mLightningCache[key]);
				}
			}
		}
		mLightningCache = newCache;

	}

	private GameObject MakeElectricity(Transform start, Transform end)
    {
		GameObject electricity = Instantiate(mElectricity, transform);
		electricity.GetComponent<ElectricityController>().player1 = start;
		electricity.GetComponent<ElectricityController>().player2 = end;
		electricity.GetComponent<ElectricityController>().Refrash();
		return electricity;
    }

	private GameObject MakeElectricityDisapear(Transform start, Transform end)
	{
		GameObject go = Instantiate(mDisapearMiddle, transform);
		Vector3 dir = (end.position - start.position).normalized;
		float angle = Mathf.Acos(dir.x) * 180 / Mathf.PI;
		if (dir.y < 0)
			angle = 360 - angle;
		go.transform.rotation = Quaternion.Euler(0, 0, angle);
		go.transform.position = (end.position + start.position) * 0.5f;
		return go;
	}

}
